package org.ufinity.hsms.module.setting;

import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class AlarmSettingActivity extends BaseActivity implements OnClickListener{
	public static final String TAG = "SettingActivity";
	private HSMApplication application;
	
	CheckBox cb_1;
	CheckBox cb_2;
	CheckBox cb_3;
	CheckBox cb_4;
	CheckBox cb_5;
	CheckBox cb_6;
	
	Button btn_save;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.setting_alarm);
		initView();
		initDate();
	}

	private void initDate() {
		SharedPreferences sharedPreferences = this.getSharedPreferences("alarm_settings", Activity.MODE_PRIVATE);
			cb_1.setChecked(sharedPreferences.getBoolean("disaster", false));
			cb_2.setChecked(sharedPreferences.getBoolean("serious", false));
			cb_3.setChecked(sharedPreferences.getBoolean("general", false));
			cb_4.setChecked(sharedPreferences.getBoolean("warning", false));
			cb_5.setChecked(sharedPreferences.getBoolean("prompt", false));
			cb_6.setChecked(sharedPreferences.getBoolean("no_type", false));
	}

	private void initView() {
		cb_1 = (CheckBox) findViewById(R.id.cb_1);
		cb_1.setOnClickListener(this);
		cb_2 = (CheckBox) findViewById(R.id.cb_2);
		cb_2.setOnClickListener(this);
		cb_3 = (CheckBox) findViewById(R.id.cb_3);
		cb_3.setOnClickListener(this);
		cb_4 = (CheckBox) findViewById(R.id.cb_4);
		cb_4.setOnClickListener(this);
		cb_5 = (CheckBox) findViewById(R.id.cb_5);
		cb_5.setOnClickListener(this);
		cb_6 = (CheckBox) findViewById(R.id.cb_6);
		cb_6.setOnClickListener(this);
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_save.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == btn_save.getId()){
			save();
		}
	}

	private void save() {
		
		SharedPreferences sharedPreference = this.getSharedPreferences("alarm_settings", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreference.edit();
		
		if(cb_1.isChecked()){
			editor.putBoolean("disaster", true);
		}else{
			editor.putBoolean("disaster", false);
		}
		
		if(cb_2.isChecked()){
			editor.putBoolean("serious", true);
		}else{
			editor.putBoolean("serious", false);
		}
		
		if(cb_3.isChecked()){
			editor.putBoolean("general", true);
		}else{
			editor.putBoolean("general", false);
		}
		
		if(cb_4.isChecked()){
			editor.putBoolean("warning", true);
		}else{
			editor.putBoolean("warning", false);
		}
		
		if(cb_5.isChecked()){
			editor.putBoolean("prompt", true);
		}else{
			editor.putBoolean("prompt", false);
		}
		
		if(cb_6.isChecked() ||(!cb_1.isChecked() && !cb_2.isChecked() && !cb_3.isChecked() && !cb_4.isChecked() && !cb_5.isChecked())){
			editor.putBoolean("no_type", true);
		}else{
			editor.putBoolean("no_type", false);
		}
		
		editor.commit();
		makeMyToast(instance, getResources().getString(R.string.success));
	}
}
